package world;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
public class Serial {

	/**this writes the contents of a serializable object or array of serializable objects (O) to the file (FILENAME). note that the
	 * object input is always written as an array of Objects (Object[])*/
	public static void writeObject(File filename, Object... o){
		try {
			OutputStream file = new FileOutputStream(filename);
			OutputStream buffer = new BufferedOutputStream(file);
			ObjectOutput output = new ObjectOutputStream(buffer);
			output.writeObject(o);
			output.close();
			buffer.close();
			file.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	/**this reads and returns the serializable object written to the file (FILENAME). note that the object is always read as an
	 * array of Objects (Object[])*/
	public static Object[] readObject(File filename){
		Object o = null;
		try {
			InputStream file = new FileInputStream(filename);
			InputStream buffer = new BufferedInputStream(file);
		    ObjectInput input = new ObjectInputStream(buffer);
		    try {
		      o = input.readObject();
		    } catch (ClassNotFoundException e) {
					e.printStackTrace();
		    }
		    file.close();
		    buffer.close();
		    input.close();
		    }
		   catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return (Object[])o;
	}

}
